<template>
  <div class="container">
    <div class="top">
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索商品信息"
        v-model="commodityNameList.name"
      >
        <template slot="prepend"
          >商品</template
        >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="commodityBtn"
        ></el-button>
      </el-input>
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索类别信息"
        v-model="classNameList.name"
      >
        <template slot="prepend"
          >类别</template
        >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="classBtn"
        ></el-button>
      </el-input>
      <el-select
        v-model="search.has_stock"
        style="margin-left: 20px"
        placeholder="请选择库存状态"
      >
        <el-option
          v-for="item in has_stockList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      统计周期:
      <el-input-number
        :controls="false"
        v-model="search.days"
        :min="1"
        controls-position="right"
        :step="1"
      ></el-input-number>
      <el-button
        size="small"
        type="primary"
        icon="el-icon-search"
        class="handleSearch"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        size="small"
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
        >清空</el-button
      >
      <el-button
        type="info"
        size="small"
        icon="el-icon-tickets"
        @click="derive()"
        >导出</el-button
      >
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
    >
      <el-table-column
        prop="whsecode"
        label="仓库编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="whsename"
        label="仓库名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="erp_goods_code"
        label="商品编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_bar_code"
        label="商品条码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_name"
        label="商品名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="category_code"
        label="分类编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="category_name"
        label="分类名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="stock_num"
        label="库存数量"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="stock_qty"
        label="库存箱数"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="order_num"
        label="出货总数"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="avg_num"
        label="日均出货数量"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="turnover_day"
        label="周转天数"
        align="center"
      ></el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <commodityDialog
      :commoditydialog="commoditydialog"
      @tag="commodityTag"
    ></commodityDialog>
    <classDialog :classdialog="classdialog" @tag="classTag"></classDialog>
  </div>
</template>
<script>
import Pagination from "@/components/pagination";
import ClassDialog from "@/components/superGrossMargin/classDialog";
import CommodityDialog from "@/components/superGrossMargin/commodityDialog";
import { goodsTurnover } from "@/api/commodity/commodityList";
import { getToken } from "@/utils/auth";

export default {
  components: {
    pagination: Pagination,
    commodityDialog: CommodityDialog,
    classDialog: ClassDialog
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper"
      },
      search: {
        days: 15,
        has_stock: ""
      },

      //dialog信息
      commoditydialog: {
        centerDialogVisible: false
      },
      classdialog: {
        centerDialogVisible: false
      },
      has_stockList: [
        {
          value: 0,
          label: "全部"
        },
        {
          value: 1,
          label: "有库存"
        }
      ],
      commodityNameList: {
        name: "",
        code: ""
      },
      classNameList: {
        name: "",
        code: ""
      },
      warehouseList: {
        name: "",
        code: ""
      }
    };
  },
  created() {
    this.getData();
  },
  methods: {
    //获取页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        category_code: this.classNameList.code,
        goods_code: this.commodityNameList.code,
        has_stock: this.search.has_stock,
        days: this.search.days
      };

      goodsTurnover(data)
        .then(res => {
          if (res.code == 0) {
            this.tableData = res.data.list.data;
            this.pageData.page = res.data.list.current_page;
            this.pageData.pagesize = res.data.list.per_page;
            this.pageData.total = res.data.list.total;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    commodityTag(data) {
      this.commodityNameList = {
        name: "",
        code: ""
      };
      console.log(data, 11111);
      let arrName = [];
      let arrCode = [];
      data.forEach(res => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });

      this.commodityNameList.name = arrName.join(",");
      this.commodityNameList.code = arrCode.join(",");
    },
    classTag(data) {
      this.classNameList = {
        name: "",
        code: ""
      };
      console.log(data, 11111);
      let arrName = [];
      let arrCode = [];
      data.forEach(res => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });
      this.classNameList.name = arrName.join(",");
      this.classNameList.code = arrCode.join(",");
    },

    //超级商品搜索
    commodityBtn() {
      this.commoditydialog = {
        centerDialogVisible: true
      };
    },
    //超级类别搜索
    classBtn() {
      this.classdialog = {
        centerDialogVisible: true
      };
    },

    // 本地搜索
    handleSearch() {
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },
    //导出
    derive() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let link = document.createElement("a");
          link.href =
            this.$axios.defaults.baseURL +
            "/smkapi/goods/exportGoodsTurnoverXLSX?token=" +
            getToken() +
            "&category_code=" +
            this.classNameList.code +
            "&goods_code=" +
            this.commodityNameList.code +
            "&days=" +
            this.search.days +
            "&has_stock=" +
            this.search.has_stock;
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出"
          });
        });
    },
    //清空
    empty() {
      this.search = {
        days: 15,
        has_stock: ""
      };
      this.classNameList.name = "";
      this.commodityNameList.name = "";
      this.classNameList.code = "";
      this.commodityNameList.code = "";
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    }
  }
};
</script>
<style scoped>
.top {
  position: relative;
  margin-bottom: 5px;
}

.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>
